<template>
  <el-page-header @back="router.go(-1)" title="返回">
    <template #content>
      <span>{{schoolForm.id ? '编辑':'添加'}}学校</span>
    </template>
  </el-page-header>

   

  <div class="content">
    <el-form ref="schoolFormRef" :model="schoolForm" :rules="schoolFormRule" :inline="false" label-width="160px">

      <el-form-item label="名称" prop="name">
        <el-input v-model="schoolForm.name" placeholder="请输入名称" maxlength="20"/>
      </el-form-item>

      <el-form-item label="地址" prop="address">
        <el-input v-model="schoolForm.address" placeholder="请输入地址" maxlength="20"/>
      </el-form-item>

      <el-form-item label="账号" prop="loginName">
        <el-input v-model="schoolForm.loginName" placeholder="请输入账号" maxlength="20"/>
      </el-form-item>

      
      <el-form-item label="联系人" prop="linkName">
        <el-input v-model="schoolForm.linkName" placeholder="请输入联系人" maxlength="20"/>
      </el-form-item>
 
      
      <el-form-item label="手机" prop="linkPhone">
        <el-input v-model="schoolForm.linkPhone" placeholder="请输入手机" maxlength="20"/>
      </el-form-item>
      

      <el-form-item>
        <el-button @click="handleSave">保存</el-button>
        <el-button type="primary" @click="handleCancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import {reactive} from "vue";
import {useRoute, useRouter} from "vue-router";

import {saveSchool,findById} from "@/api/school"
   
import { ElMessage } from 'element-plus'

const router = useRouter();
const route = useRoute();
 

let schoolFormRef = reactive(null);
 
let schoolForm = reactive({
  id:null,
  name: null,
  address:null,
  loginName:null,
  linkName:null,
  linkPhone:null
});
let schoolFormRule = reactive({
  name: [
    {
      required: true,
      trigger: 'blur'
    }
  ]
})
 
     
if(route.query.schoolId){
  //根据id查找
  findById(route.query.schoolId).then((resp)=>{
  console.log('resp', resp)
    let school = resp.data;
    schoolForm.id = school.id;
    schoolForm.name = school.name;
    schoolForm.address = school.address;
    schoolForm.loginName = school.loginName;
    schoolForm.linkName = school.linkName;
    schoolForm.linkPhone = school.linkPhone;

  }).catch((error)=>{

  })
} 

let handleSave = () => {
  //判断是否校验通过
  schoolFormRef.validate((valid) => {
    if (valid) { 
      saveSchool(schoolForm.id,schoolForm).then((resp) => {
        //提示并且返回列表
        router.push({path: '/school/list'});
      });
    }
  });
}
</script>

<style scoped>
.content {
  margin-top: 40px;
}
</style>
 